package com.alpha.framework.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.alpha.framework.R;
import com.alpha.framework.activity.BaseActivity;

public abstract class BaseFragmentActivity extends BaseActivity implements OnClickListener {

	// public FrameLayout mUserFrame;
	private TextView mBack;
	private TextView mTitle;
	private TextView mOperation;

	private FragmentManager mFragmentManager;

	public BaseFragmentActivity() {
		mFragmentManager = getSupportFragmentManager();
	}

	@Override
	protected void initView() {

		setContentView(R.layout.lib_default_layout);
		mBack = (TextView) findViewById(R.id.tv_titlebar_back);
		mTitle = (TextView) findViewById(R.id.tv_titlebar_title);
		mOperation = (TextView) findViewById(R.id.tv_titlebar_operation);

		mBack.setOnClickListener(this);
		mOperation.setOnClickListener(this);

		initBundle();
		initDefaultView();
	}

	/**
	 * 初始化fragment
	 */
	protected abstract void initDefaultView();
	
	/**
	 * 初始化 Fragment 的数据
	 */
	protected abstract void initBundle();


	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.tv_titlebar_back){
			titleBack();
		}else if(id == R.id.tv_titlebar_operation){
			titleOperation();
		}
	}

	/**
	 * 设置 title back 是否显示
	 * @param isVisibleMode
	 */
	public void setTitleBackVisible(int isVisibleMode){
		mBack.setVisibility(isVisibleMode);
	}
	
	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	public void setTitle(String title) {
		mTitle.setText(title);
	}

	/**
	 * 设置 title operation
	 * @param operationName
	 */
	public void setTitleOperation(String operationName) {
		mOperation.setText(operationName);
	}

	/**
	 * title back 单击时回调现在处于前台的Fragment
	 */
	private void titleBack() {
		if(null!=BaseFragment.getForegroundFragment())
			BaseFragment.getForegroundFragment().onTitleBackClick();
	}

	/**
	 * title operation 单击时回调现在处于前台的Fragment
	 */
	private void titleOperation() {
		if(null!=BaseFragment.getForegroundFragment())
			BaseFragment.getForegroundFragment().onTitleOperationClick();
	}
	
	
	/**
	 * 切换Fragment
	 * 默认 将Fragment 加入回退栈
	 * 默认 Bundle 为null
	 * @param clazz
	 * @param bundle
	 */
	protected void switchFragment(Class<? extends BaseFragment> clazz){
		switchFragment(true, clazz, null);
	}
	
	/**
	 * 切换Fragment
	 * 默认 将Fragment 加入回退栈
	 * @param clazz
	 * @param bundle
	 */
	protected void switchFragment(Class<? extends BaseFragment> clazz,Bundle bundle){
		switchFragment(true, clazz, bundle);
	}
	
	/**
	 * 切换Fragment 
	 * @param isAddBackStack 是否加入回退栈
	 * @param clazz 切换的Fragment Class
	 * @param bundle 传递的数据
	 */
	public void switchFragment(boolean isAddBackStack,Class<? extends BaseFragment> clazz,Bundle bundle){
	
		BaseFragment fragment = BaseFragment.findFragment(clazz.getName());
		if(null==fragment ){
			fragment=instanceFragment(clazz);
		}
		if(null!=bundle)
			fragment.setArguments(bundle);
		replaceFragment(isAddBackStack, fragment);
	}

	/**
	 * 实例化 BaseFragment Class 子类
	 * @param clazz
	 * @return
	 */
	private BaseFragment instanceFragment(Class<? extends BaseFragment> clazz){
		try {
			return clazz.newInstance();
		} catch (Exception e) {
			new RuntimeException("实例化的Fragment 错误！");
		}
		return null;
	}
	
	/**
	 * 替换Fragment
	 */
	private void replaceFragment(boolean isAddBackStack, BaseFragment fragment) {
		FragmentTransaction mBeginTransaction = mFragmentManager.beginTransaction();
		mBeginTransaction.replace(android.R.id.tabcontent, fragment, fragment.getClass().getName());
		if (isAddBackStack) {
			mBeginTransaction.addToBackStack(null);
		}
		mBeginTransaction.commit();
	}
	
	/**
	 * 清除保存Fragment 的集合
	 * 清除 Fragment回退栈
	 */
	public void clearSaveFragment(){
		BaseFragment.finishFragment();
		mFragmentManager.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//清除Fragment
		BaseFragment.finishFragment();
	}
	
}
